
// 引入定义的函数
import { defineStore } from "pinia";
// 引入接口函数
import { reqgetCategory1ListData, reqgetCategory2ListData, reqgetCategory3ListData } from '@/api/product/category'
//定义类型约束
import type { CategorySelectorStateModel } from "@/api/product/model/CategoryModel"


// 创建函数
export const useCategorySelectorStore = defineStore('querySelector', {

  // state数据
  state: (): CategorySelectorStateModel => {
    return {
      category1List: [],
      category2List: [],
      category3List: [],
      category1Id: undefined,
      category2Id: undefined,
      category3Id: undefined,

    }


  },
  actions: {

    // 获取一级分类
    async getCategory1List() {
      const res = await reqgetCategory1ListData()

      // 将我返回的数据存储到一级list
      this.category1List = res;


    },
    // 获取二级分类的
    async getCategory2List(category1Id: number) {
      const res = await reqgetCategory2ListData(category1Id)
      //将数据存入二级的lisT中，
      this.category2List = res
      this.category1Id = category1Id
      // 让二级分类下拉框重置，或者不在显示
      this.category2Id = undefined
      this.category3Id = undefined
      this.category3List = []

    },
    //获取三级的
    async getCategory3List(category2Id: number) {
      const res = await reqgetCategory3ListData(category2Id)
      //将数据存入二级的lisT中，
      this.category3List = res
      this.category2Id = category2Id
      this.category3Id = undefined
    },
    //保存三级的id
    SaveCategory3Id(category3Id: number) {
      console.log(category3Id)
      this.category3Id = category3Id;
    }
  },
  getters: {

  }



})